// pages/scan-code-pay/scan-code-pay.js
Page({

    /**
     * 组件的初始数据
     */
    data: {
        is_focus: false,
        price: '',
        remark: '',
        must_price: 0,
        coupon: '',
        coupon_info: '',
        pay_list: [],
        coupon_list: [],
        show_coupon_picker: false,
        picker_coupon: -1,
        goods_id: 0,
        mch_list: {},
        mch_id: 0,
        mch: null,
    },

    onLoad: function (e) {
        getApp().page.onLoad(this, e);
        if (e.price && e.price > 0) {
            this.setData({
                price: e.price
            })
            this.onBlur();
        }
        if (e.mch_id && e.mch_id > 0 || e.is_pay) {
            getApp().core.setNavigationBarTitle({
                title: '买单'
            });
            this.setData({
                mch_id: e.mch_id
            })
        }
        if (e.scene) {
          var scene = decodeURIComponent(e.scene);
          scene = getApp().helper.scene_decode(scene);
          if (scene.p && scene.p > 0) {
            this.setData({
              price: parseFloat(scene.p)
            })
            this.onBlur();
          }
          if (scene.m && scene.m > 0) {
            getApp().core.setNavigationBarTitle({
              title: '买单'
            });
            this.setData({
              mch_id: parseInt(scene.m)
            })
          }
        }
        this.getMch();
    },

    onReady: function (e) {
        getApp().page.onReady(this);
    },

    onShow: function (e) {
        getApp().page.onShow(this);
    },

    getMch: function () {
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        });
        var that = this;
        getApp().request({
            url: getApp().api.scan_code_pay.get_mch,
            data: {
                mch_id: this.data.mch_id
            },
            success: function (e) {
                if (e.code == 0) {
                    that.setData({
                        mch: e.data.mch
                    });
                }
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },

    onInput: function (e) {
        var t = '{"' + e.currentTarget.dataset.name + '":"' + e.detail.value + '"}';
        this.setData(JSON.parse(t));
        if (e.currentTarget.dataset.name == 'price') {
            this.onBlur()
        }
    },
    onFocus: function () {
        this.setData({
            is_focus: true
        });
    },
    onBlur: function (e) {
        var self = this;
        if (this.data.price == '' || this.data.price < 0.01) {
            this.setData({
                is_focus: false,
                coupon_info: '暂无优惠',
                coupon: '暂无可用优惠券',
                must_price: 0
            });
            return;
        }
        getApp().request({
            url: getApp().api.scan_code_pay.get_coupon,
            data: {
                price: this.data.price,
                mch_id: this.data.mch_id ? this.data.mch_id : 0
            },
            success: function (e) {
                if (e.code == 0) {
                    self.setData({
                        'pay_list': e.data.payType_list,
                        'coupon_list': e.data.coupon_list,
                        'goods_id': e.data.goods_id
                    });
                    if (e.data.coupon_list.length == 0) {
                        self.setData({
                            coupon_info: '暂无优惠',
                            coupon: '暂无可用优惠券',
                            must_price: this.data.price
                        });
                    } else {
                        var select_coupon = e.data.coupon_list[0];
                        var must_price = (self.data.price - select_coupon.sub_price).toFixed(2);
                        self.setData({
                            coupon_info: '优惠' + select_coupon.sub_price + '元',
                            coupon: '满' + select_coupon.min_price + '减' + select_coupon.sub_price + '元',
                            picker_coupon: select_coupon,
                            must_price: must_price <= 0 ? 0.01 : must_price
                        });
                    }
                } else {
                    wx.showModal({
                        title: '提示',
                        content: e.msg
                    });
                }
            },
            complete: function () {
                self.setData({
                    is_focus: false
                });
            }
        });
    },
    pickCoupon: function (e) {
        var index = e.currentTarget.dataset.index
        var coupon_info = '';
        var coupon = '';
        var picker_coupon = -1;
        var must_price = 0;
        if (index == -1) {
            if (this.data.coupon_list.length > 0) {
                coupon = '有' + this.data.coupon_list.length + '张优惠券可用';
                coupon_info = '暂无优惠';
            } else {
                picker_coupon = -1;
                coupon_info = '暂无优惠';
                coupon = '暂无可用优惠券';
            }
            must_price = this.data.price;
        } else {
            var select_coupon = this.data.coupon_list[index];
            picker_coupon = select_coupon;
            coupon_info = '优惠' + select_coupon.sub_price + '元';
            coupon = '满' + select_coupon.min_price + '减' + select_coupon.sub_price + '元';
            must_price = (this.data.price - select_coupon.sub_price).toFixed(2);
        }

        this.setData({
            coupon: coupon,
            coupon_info: coupon_info,
            picker_coupon: picker_coupon,
            show_coupon_picker: false,
            must_price: must_price <= 0 ? 0.01 : must_price
        });
    },
    select_coupon: function () {
        if (this.data.is_focus) {
            return;
        }
        if (this.data.price == '') {
            wx.showToast({
                title: '请先输入金额',
                icon: 'none'
            });
            return;
        }
        if (this.data.coupon_list.length == 0) {
            wx.showToast({
                title: '暂无可用优惠券',
                icon: 'none'
            });
            return;
        }
        this.setData({
            show_coupon_picker: true
        });
    },
    onSubmit: function () {
        if (this.data.is_focus) {
            return;
        }
        this.getOrderData();
        
    },
    getOrderData: function () {
        var self = this;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        });
        var mch_list = [{
            "mch_id": self.data.mch_id,
            "goods_list": [{
                "goods_id": this.data.goods_id,
                "num": 1,
                "attr": [{
                    "attr_group_id": 1,
                    "attr_id": 1
                }]
            }]
        }];
        var data = {
            address_id: '',
            longitude: '',
            latitude: '',
            mch_list: JSON.stringify(mch_list)
        };
        getApp().request({
            url: getApp().api.order.new_submit_preview,
            method: "POST",
            data: data,
            success: function (res) {
                if (res.code == 0) {
                    var mch_list = res.data.mch_list
                    mch_list[0].total_price = self.data.price;
                    mch_list[0].level_price = self.data.price;
                    mch_list[0].show = false;
                    mch_list[0].show_length = 0;
                    mch_list[0].shop = {},
                    mch_list[0].picker_coupon = self.data.picker_coupon == -1 ? {} : self.data.picker_coupon;
                    mch_list[0].offline = 0;
                    mch_list[0].coupon_list = self.data.coupon_list;
                    mch_list[0].plugin_type = 6;
                    mch_list[0].content = self.data.remark;
                    self.setData({
                        mch_list: mch_list
                    });
                    self.showPay();
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.msg
                    });
                }
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    showPay: function () {
        var self = this;
        var select_list = new Array();
        for (var i = 0; i < this.data.pay_list.length; i++) {
            select_list.push(this.data.pay_list[i].name);
        }
        wx.showActionSheet({
            itemList: select_list,
            success: function (res) {
                var payment = self.data.pay_list[res.tapIndex].payment;
                var data = {
                    address_id: 0,
                    formId: undefined,
                    formIds: '[]',
                    mch_list: JSON.stringify(self.data.mch_list),
                    payment: payment,
                    use_integral: 1
                }
                self.order_submit(data, 's');
            }
        });
    }
})
